﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;

namespace AMSService.model
{
    [DataContract (IsReference=true)]//(Namespace="AMSService.model")]
    public class ServiceData : DBConnector
    {
        private int _id = 0;
        private DateTime _dt;
        private string _comp;
        private string _title;
        private double _price;
        private string _comments;
        private bool _haswarr;
        private Asset _asset;
        private List<AMSImage> _images;

        // Stored procedure names
        private const string _spgetservbyid = "GetServiceById";
        private const string _spdelservbyid = "DeleteServiceById";
        private const string _spinsserv = "InsertService";
        private const string _spupdserv = "UpdateService";

        // Stored procedure arguments
        private const string _prmid = "@id";
        private const string _prmarg = "@arg";
        private const string _prmdate = "@servdate";
        private const string _prmcomp = "@company";
        private const string _prmcost = "@cost";
        private const string _prmcomm = "@comments";
        private const string _prmwarr = "@underwarr";
        private const string _prmttl = "@title";
        private const string _prmassetid = "@assetid";

        // Table field names
        private const string _fldid = "pkid";
        private const string _flddate = "servdate";
        private const string _fldcomp = "company";
        private const string _fldprice = "cost";
        private const string _fldcomm = "comments";
        private const string _fldwarr = "underwarr";
        private const string _fldttl = "title";
        private const string _fldassetid = "fkassetid";

        public enum _spargs { id, assetid };

        public ServiceData(Asset BaseItem)
        {
            _asset = BaseItem;
            _id = 0;
            InitializeAttributes();
        }

        public ServiceData(Asset BaseItem, int ID)
        {
            _asset = BaseItem;
            InitializeAttributes();
            FillServData(ID);
        }

        public ServiceData(Asset BaseItem, DataRow Row)
        {
            _asset = BaseItem;
            InitializeAttributes();
            FillServData(Row);
        }

        public static string SpSelect
        {
            get { return _spgetservbyid; }
        }

        public static string SpPrmId
        {
            get { return _prmid; }
        }

        public static string SpPrmArg
        {
            get { return _prmarg; }
        }

        [DataMember]
        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }

        [DataMember]
        public DateTime Date
        {
            get { return _dt; }
            set { _dt = value; }
        }

        [DataMember]
        public string Company
        {
            get { return _comp; }
            set { _comp = value; }
        }

        [DataMember]
        public double Price
        {
            get { return _price; }
            set { if (value >= 0) { _price = value; } }
        }

        [DataMember]
        public string Comments
        {
            get { return _comments; }
            set { _comments = value; }
        }

        [DataMember]
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        [DataMember]
        public bool UnderWarranty
        {
            get { return _haswarr; }
            set { _haswarr = value; }
        }

        [DataMember]
        public Asset BaseItem
        {
            get { return _asset; }
            set { if (value != null) { _asset = value; } }
        }

        [DataMember]
        public List<AMSImage> Images
        {
            get { return _images; }
            private set { }
        }

        public bool Delete()
        {
            List<SqlParameter> paramlist = new List<SqlParameter>();
            SqlParameter pid = new SqlParameter(_prmid, _id);
            paramlist.Add(pid);

            return SendWriteRequest(_spdelservbyid, paramlist) > 0;
        }

        private void FillServData(DataRow dr)
        {
            _id = Convert.ToInt32(dr[_fldid].ToString());
            _dt = Convert.ToDateTime(dr[_flddate].ToString());
            _comp = dr[_fldcomp].ToString();
            _price = Convert.ToDouble( dr[_fldprice].ToString());
            _comments = dr[_fldcomm].ToString();
            _haswarr = Convert.ToBoolean(dr[_fldwarr].ToString());
            _title = dr[_fldttl].ToString();
        }

        private void FillServData(int ID)
        {
            List<SqlParameter> paramlist = new List<SqlParameter>();
            SqlParameter pid = new SqlParameter(_prmid, _id);
            SqlParameter parg = new SqlParameter(_prmarg, (int)_spargs.id);
            paramlist.Add(pid);
            paramlist.Add(parg);
            DataTable dt = SendReadRequest(_spgetservbyid, paramlist);

            if (dt.Rows.Count > 0)
            {
                FillServData(dt.Rows[0]);
            }
        }

        private void InitializeAttributes()
        {
            _images = new List<AMSImage>();
        }

        public void LoadImages()
        {
            DataTable dt = SendReadRequest(AMSImage.SpSelect,
                new SqlParameter[] { new SqlParameter(AMSImage.SpPrmId, _id),
                                     new SqlParameter(AMSImage.SpPrmArg, AMSImage._spargs.servid) });

            if (_images == null)
                _images = new List<AMSImage>();
            else
                _images.Clear();

            foreach (DataRow dr in dt.Rows)
                _images.Add(new AMSImage(_asset, this, dr));
        }

        public bool Save()
        {
            List<SqlParameter> paramlist = new List<SqlParameter>();
            SqlParameter pid = new SqlParameter(_prmid, _id);
            SqlParameter pdate = new SqlParameter(_prmdate, _dt);
            SqlParameter pcomp = new SqlParameter(_prmcomp, _comp);
            SqlParameter pprice = new SqlParameter(_prmcost, _price);
            SqlParameter pcomm = new SqlParameter(_prmcomm, _comments);
            SqlParameter pwarr = new SqlParameter(_prmwarr, _haswarr);
            SqlParameter passetid = new SqlParameter(_prmassetid, _asset.ID);

            paramlist.Add(pdate);
            paramlist.Add(pcomp);
            paramlist.Add(pprice);
            paramlist.Add(pcomm);
            paramlist.Add(pwarr);
            paramlist.Add(passetid);

            // If this is an update request (the id already exists)
            if (_id > 0)
            {
                paramlist.Add(pid);
                return SendWriteRequest(_spupdserv, paramlist) > 0;
            }
            else
            {
                _id = SendWriteRequest(_spinsserv, paramlist);
                return _id > 0;
            }
        }
    }
}